//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData: {
    userInfo: null
  }
})

var app = getApp()
Page({
  data: {
    motto: 'Hello ! WelCome to WxApp',
    islogin: false,
    userInfo: {},
    menuitems: [
      // { text: '我的租房', url: '../wdzf/wdzf', icon: '../../image/zufang.png', tips: '1' },
      // { text: '历史租房', url: '../lszf/lszf', icon: '../../image/lishi.png', tips: '2' },
      { text: '我的收藏', url: '../wdsc/wdsc', icon: '../../image/shoucang.png', tips:''},
      { text: '关于我们', url: '../gywm/gywm', icon: '../../image/guanyu.png', tips: '' }
    ]
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  todetail:function(){
    var userInfo = JSON.stringify(this.data.userInfo);
    wx.navigateTo({
      url: '../medetail/medetail?userInfo='+userInfo
    })
  },
  onLoad: function () {
    // console.log('onLoad')
     var that = this;
    // var CuserInfo = wx.getStorageSync('CuserInfo');
    // if (CuserInfo.accesstoken) {
    //   that.setData({ islogin: true });
    // }
    // console.log(CuserInfo)

    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    }) 
  }
})